﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Controls;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TXTXKON : ViewBase
    {
        #region ==PIND===========
        public static readonly DependencyProperty PINDProperty = DependencyProperty.Register("PIND",
            typeof(string), typeof(TXTXKON),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPINDChanged));
        public string PIND
        {
            get
            {
                return (string)GetValue(PINDProperty);
            }
            set
            {
                SetValue(PINDProperty, value);
            }
        }
        private static void OnPINDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TXTXKON;
            if (src != null)
            {
                src.PIND = (string)e.NewValue;
                src.Load();
            }
        }
        #endregion
        Dictionary<string, string> MapTxt = new Dictionary<string, string>();
        public TXTXKON()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_TXTKON();
            IsMasterFromMV = true;
            TBMain = MV.SetProd.Tables[TB];
            InitializeComponent();
            MapTxt.Add("010", "1799PVA");
            MapTxt.Add("020", "P24");
            MapTxt.Add("030", "JD-136C");
            MapTxt.Add("040", "淀粉");
            MapTxt.Add("050", "丙烯");
            MapTxt.Add("060", "抗静电剂");
            MapTxt.Add("070", "蜡片");
            MapTxt.Add("080", "含固量");
            MapTxt.Add("090", "回潮率");           
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }


        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMaster == null)
                return;
        }
        DataSet SResult;
        VTXTIND TK;
        VTXTKON KON;
        public void Load()
        {
            if (TBMain == null)
                return;
            TBMain.Clear();
            if (PIND.Length == 0)
                return;
            if (TK == null)
                TK = new VTXTIND();
            if (KON == null)
                KON = new VTXTKON();


            DataRow dwTXT = TBMain.NewRow();
            DataRow dwData = TBMain.NewRow();
            dwData = TK.SetProd.Tables["TXTIND"].NewRow();
            dwData["TXTIMANDANT"] = app.Mandant;
            dwData["TXTITXTART"] = "ABP";
            dwData["TXTISIND"] = PIND;
            SResult = KON.QueryTXTKON(dwData, 1);
            SResult = ConvertTXT(SResult.Tables["TXTKON"], dwTXT);
            MergeData(SResult, true, true);
        }
        public DataSet ConvertTXT(DataTable tbTXT, DataRow dwTarget)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            Dictionary<string, int> txtID = new Dictionary<string, int>();
            string processed = "";
            DataRow dwTXTKON = null;
            DataTable tbTXTKON = dwTarget.Table.Clone();
            int pos = 1;
            string txt = "";
            int idx = -1;
            string spliter = "     ";
            dwTXTKON = tbTXTKON.NewRow();
            string value = "";
            int idx1 = -1;
            string txtn = "";
            foreach (DataRow dwTXT in tbTXT.Rows)
            {
                txt = Helper.GetGB2312Value(dwTXT["TXTKTEXT"].ToString());
                foreach (KeyValuePair<string, string> kp in MapTxt)
                {
                    string key = kp.Key;//010
                    string content = kp.Value;//JD-136C
                    idx = txt.IndexOf(content);//-1
                    if (idx >= 0)
                    {
                        idx1 = txt.IndexOf(spliter, idx);
                        if (idx1 >= 0)
                        {
                            //3，JD-136C：25，‘      ’，18
                            txtn = txt.Substring(idx, idx1 - idx); //JD-136C：25
                        }
                        else
                        {
                            txtn = txt.Substring(idx);
                        }
                        dwTXTKON["TXT" + key] = txtn.Replace(content, "").Replace( ":", "").Replace( "：", "").Replace("(", "").Replace(")", "").Replace("KG", "").Replace("%",""); 
                    }
                }
            }
            tbTXTKON.Rows.InsertAt(dwTXTKON, 0);
            SReturn.Merge(tbTXTKON);
            return SReturn;
        }
    }
}
